package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.service.ISystemEventLogService;
import com.group.project.hrms.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Controller
public class SystemEventLogController {

    @Autowired
    private ISystemEventLogService iSystemEventLogService;

    @Autowired
    private DateUtil dateUtil;


    /**
     * 接收前端请求，按照指定日期段查询系统操作日志
     * @param map 日期段
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doGetSystemEventLogsByDateRange")
    @ResponseBody
    public ResultEntity doGetSystemEventLogsByDateRange(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object opContactID = request.getAttribute("contactId");
        Object staffDingID = request.getAttribute("staffDingId");
        Object staffName = request.getAttribute("staffName");
        if(null != map){
            Object dateRange = map.get("dateRange"); //[2022-06-01, 2022-06-04]
            if(null != dateRange){
                JSONArray dateArray = JSON.parseArray(JSON.toJSONString(dateRange));
                String startDate = dateArray.getString(0) + " 00:00:00";
                String endDate = dateArray.getString(1) + " 23:59:59";
                resultEntity.setDataList(iSystemEventLogService.selectSystemEventLogs(dateUtil.formatStringToDate(startDate), dateUtil.formatStringToDate(endDate)));
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }


}
